package android.fwirk.facebook;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FacebookStatus {
	int uid;
	String statusID; 	
	Date time; 	
	String source; 	
	String message;
	String postID; 	
	int viewerID; 	
	int appID; 	
	int sourceID; 	
	Date updatedTime=null; 	
	Date createdTime=null; 	
	String filterKey; 	
	String attribution; 	
	String actorID; 	
	String targetID; 	
    int  likecount;
    String attachDesc;
    String images;
	boolean isHidden; 
	FacebookUser owner;
	final String IMG_START="<img src='";
	final String IMG_END="' alt=''></img>";
	
	public FacebookStatus(JSONObject status) {
		parse(status);
	}
	
	public void parse(JSONObject status) {
		uid=status.optInt("uid", 0);
		statusID=status.optString("status_id", "");
		String stime=status.optString("time", "");
		if(!stime.equals("")) {
			updatedTime=new Date(Long.parseLong(stime)*1000); 	
		}
		source=status.optString("source", "");
		message=status.optString("message", "");
		if(message.equals("")) {
			message=status.optString("text", "");
		}
		postID=status.optString("post_id", "");	
		viewerID= status.optInt("viewer_id", 0);
		appID=status.optInt("app_id", 0);
		sourceID=status.optInt("source_id", 0);
		String utime=status.optString("updated_time", "");
		String ctime=status.optString("created_time", "");
		if(!utime.equals("") && !ctime.equals("")) {
			updatedTime=new Date(Long.parseLong(utime)*1000); 	
			createdTime=new Date(Long.parseLong(ctime)*1000);
		}
		if(utime.equals("")) {
			utime=status.optString("time", "");
			if(!utime.equals("")) {
				updatedTime=new Date(Long.parseLong(utime)*1000); 	
			}
		}
		images="";
		JSONObject attachment=status.optJSONObject("attachment");
		if(attachment!=null) {
			JSONArray media=attachment.optJSONArray("media");
			if(media!=null) {
				for(int x=0;x<media.length();x++) {
					JSONObject mObj=media.optJSONObject(x);
					if(mObj!=null) {
						String img=mObj.optString("src","").replace("\\", "");
						if(!img.contains("app_full_proxy") && (img.contains(".jpg") || img.contains(".gif") || img.contains(".png"))) {
							images=images+IMG_START+img+IMG_END;
						}
					}
				}
			}
			attachDesc=attachment.optString("name","");
			if(!attachDesc.equals("")) {
				attachDesc=attachDesc+"<br/>";
			}
			attachDesc=attachDesc+attachment.optString("caption","");
			if(!attachDesc.equals("")) {
				attachDesc=attachDesc+"<br/>";
			}
			attachDesc=attachDesc+"<br/>"+attachment.optString("description","");
		}
		else {
			attachDesc="";
		}
		JSONObject app_data=status.optJSONObject("app_data");
		if(app_data!=null) {
			String attachData=app_data.optString("attachment_data","");
			attachData=attachData.replace("\\", "");
			if(attachData.startsWith("{")) {
				JSONObject attach=null;
				try {
					attach=new JSONObject(attachData);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if(attach!=null) {
					JSONObject iconObj=attach.optJSONArray("media").optJSONObject(0);
					if(iconObj!=null) {
						String img=iconObj.optString("src","");
						if(img.contains(".jpg") || img.contains(".gif")  || img.contains(".png")) {
							images=images+IMG_START+img+IMG_END;
						}
					}
				}
			}
		}
		filterKey=status.optString("filter_key", ""); 	
		attribution=status.optString("attribution", ""); 	
		actorID=status.optString("actor_id", "");
		if(actorID.equals("")) {
			actorID=status.optString("fromid", "");
		}
		targetID=status.optString("target_id", ""); 	
	    JSONArray likes=status.optJSONArray("likes");
		if(likes!=null) {
			likecount=likes.optInt(1,0);
		}
		isHidden=status.optBoolean("is_hidden",false); 
	}
	
	public static List<FacebookStatus> getFacebookStatuses(JSONArray status) {
		LinkedList<FacebookStatus> list=null;
		if(status!=null) {
			if(status.length()>0) {
				list=new LinkedList<FacebookStatus>();
			}
			for (int x = 0; x < status.length(); x++) {
				FacebookStatus s;
				try {
					s = new FacebookStatus(status.getJSONObject(x));
					list.add(s);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getStatusID() {
		return statusID;
	}

	public void setStatusID(String statusID) {
		this.statusID = statusID;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPostID() {
		return postID;
	}

	public void setPostID(String postID) {
		this.postID = postID;
	}

	public int getViewerID() {
		return viewerID;
	}

	public void setViewerID(int viewerID) {
		this.viewerID = viewerID;
	}

	public int getAppID() {
		return appID;
	}

	public void setAppID(int appID) {
		this.appID = appID;
	}

	public int getSourceID() {
		return sourceID;
	}

	public void setSourceID(int sourceID) {
		this.sourceID = sourceID;
	}

	public Date getUpdatedTime() {
		return updatedTime;
	}

	public void setUpdatedTime(Date updatedTime) {
		this.updatedTime = updatedTime;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public String getFilterKey() {
		return filterKey;
	}

	public void setFilterKey(String filterKey) {
		this.filterKey = filterKey;
	}

	public String getAttribution() {
		return attribution;
	}

	public void setAttribution(String attribution) {
		this.attribution = attribution;
	}

	public String getActorID() {
		return actorID;
	}

	public void setActorID(String actorID) {
		this.actorID = actorID;
	}

	public String getTargetID() {
		return targetID;
	}

	public void setTargetID(String targetID) {
		this.targetID = targetID;
	}

	public int getLikecount() {
		return likecount;
	}

	public void setLikecount(int likecount) {
		this.likecount = likecount;
	}

	public boolean isHidden() {
		return isHidden;
	}

	public void setHidden(boolean isHidden) {
		this.isHidden = isHidden;
	}

	public FacebookUser getOwner() {
		return owner;
	}

	public void setOwner(FacebookUser owner) {
		this.owner = owner;
	}

	public String getAttachDesc() {
		return attachDesc;
	}

	public void setAttachDesc(String attachDesc) {
		this.attachDesc = attachDesc;
	}


	public String getImages() {
		return this.images;
	}

	public void setImages(String images) {
		this.images = images;
	}
	
	
}
